﻿<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Web报表(B/S报表)演示 - 自定义汇总统计报表</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<script src="../../CreateControl.js" type="text/javascript"></script>
	<script src="../../GRUtility.js" type="text/javascript"></script>
	<script type="text/javascript">
var BeginSumDic;
var Report; 

function window_onload() 
{
    ReportViewer.Stop();
    
    Report = ReportViewer.Report;
    
    //关联报表事件
    Report.OnGroupEnd = OnGroupEnd;
    
    //从网上取XML数据并转存到一个动态创建的记录集中
    var varRecordset = Report.Utility.CreateRecordset();
    var fldProductID = varRecordset.AddField("ProductID", 2/*整数类型*/); 
    var fldQuantity = varRecordset.AddField("Quantity", 3/*浮点数字类型*/);
    var fldAmount = varRecordset.AddField("Amount", 3/*浮点数字类型*/);
    varRecordset.LoadData("xmlBeginSum.jsp");

    //创建Dictionary,并将期初值存入其中
    BeginSumDic = new Array(varRecordset.RecordCount);
	varRecordset.First();
	var No = 0;
	while ( !varRecordset.Eof() )
	{
	    var ProductID = fldProductID.AsString;
		var Qty = fldQuantity.AsFloat;
		var Amt = fldAmount.AsFloat;
		
		var obj = new Object();
		obj.ProductID = ProductID;
		obj.Qty = Qty;
		obj.Amt = Amt;
		BeginSumDic[No] = obj;
		
	    varRecordset.Next();
	    ++No;
	}
        
    //启动报表运行
    ReportViewer.Start();
}

function GetBeginSum(ProductID)
{
    //用折半查找法进行查找
    var Count = BeginSumDic.length;
    var begin = 0;
    var end = Count-1;
    while (begin <= end)
    {
        var mid = (begin + end);
        mid = (mid - (mid%2)) / 2;
        if (BeginSumDic[mid].ProductID == ProductID)
            return BeginSumDic[mid];
        else if(BeginSumDic[mid].ProductID < ProductID)
            begin = mid+1;
        else
            end = mid-1;
    }
    return null;
}
                
function OnGroupEnd(Sender)
{
	var ProductID = Report.FieldByName("ProductID").AsInteger;
	var BeginQty = 0;
	var BeginAmt = 0;
	var BeginSum = GetBeginSum(ProductID);
    if ( BeginSum != null )
	{
		BeginQty = BeginSum.Qty;
		BeginAmt = BeginSum.Amt;
	}

	Report.Parameters.Item("BeginQty").AsFloat = BeginQty;
	Report.Parameters.Item("BeginAmt").AsFloat = BeginAmt;

	var YearSumQtyBox = Report.ControlByName("YearSumQty").AsSummaryBox;
	YearSumQtyBox.Value = YearSumQtyBox.Value + BeginQty;
	var YearSumAmtBox = Report.ControlByName("YearSumAmt").AsSummaryBox;
	YearSumAmtBox.Value = YearSumAmtBox.Value + BeginAmt;
}
    </script>
    <style type="text/css">
        html,body {
            margin:0;
            height:100%;
        }
    </style>
</head>
<body style="margin:0" onload="return window_onload()">
	<script type="text/javascript"> 
	    //CreatePrintViewerEx("100%", "100%", "CustomSummary.grf", "xmlCustomSummary.jsp", false, "");
	    CreateDisplayViewerEx("100%", "100%", "CustomSummary.grf", "../AdvDataCenter.jsp?data=CustomSummary", false, "");
	</script>
</body>
</html>
